<template>
	<el-dialog title="新增" :visible.sync="dialogFormVisible" :before-close="handleDialogClose" width="60%">
		<el-form :model="form" :rules="rules" ref="form" v-if="cleardata">
			<el-form-item label="帖子标题" :label-width="formLabelWidth" prop="postName">
				<el-input v-model="form.postName" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="帖子封面" :label-width="formLabelWidth" prop="contentCover">
				<el-upload class="orders-img_el_upload" show-file-list :action="upurl" :limit="limitCountImg" list-type="picture-card"
					:on-change="handleChange" :on-preview="handlePreview" :on-remove="handleRemove"
					:on-success="handleSuccess" :on-error="handleError" :file-list="fileList" 
					:class="{ disUoloadSty: noneBtnImg }" ref="upload" :disabled="uploadDisabled"
					@click.stop="submitUpload">
					<span class="orders-img_el_upload_btn">添加图片(最多三张)</span>
				</el-upload>
			</el-form-item>
			<el-form-item label="帖子内容" :label-width="formLabelWidth" prop="postContent">
				<div ref="editor"></div>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="cleardata">取 消</el-button>
			<el-button type="primary" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>

<script>
	import postApi from "@/api/posts.js"
	import Editor from "wangeditor"
	export default {
		data() {
			return {
				editor: null,
				dialogFormVisible: false,
				uploadDisabled: false,
				noneBtnImg: false,
				limitCountImg: 3,
				upurl: "/api/file/uploadList/posts",
				fileList: [],
				form: {
					postName: "",
					postContent: "",
					contentCover: ""
				},
				rules: {
					postName: [{
						required: true,
						message: "请输入帖子标题",
						trigger: 'blur'
					}]
				},
				formLabelWidth: '120px'
			};
		},
		methods: {
			async addPost() {
				let content = this.editor.txt.html();
				console.log(content);
				const res = await postApi.addPost("/znExchangePost/addPost", {
					postName: this.form.postName,
					postContent: this.form.postContent = content,
					contentCover: this.form.contentCover
				});
				if (res.code == 200) {
					this.dialogFormVisible = false;
					this.$message.success(res.msg);
					this.$parent.SearchPostsAll();
				} else {
					this.$message.error(res.msg);
				}
			},
			createEditor() {
				this.$nextTick(() => {
					this.editor = new Editor(this.$refs.editor)
					this.editor.config.uploadImgServer = "api/file/editUpload/posts"
					this.editor.config.uploadFileName = "file"
					this.editor.config.uploadVideoServer = 'api/file/editVideoUpload/posts'
					this.editor.config.uploadVideoName = "file"
					this.editor.create()
				})
			},
			cleardata() {
				this.dialogFormVisible = false;
				this.$refs.form.resetFields();
				this.editor.destroy();
				this.editor = null;
			},
			handleDialogClose() {
				this.dialogFormVisible = false;
				this.$refs.form.resetFields();
				this.editor.destroy();
				this.editor = null;
			},
			handleError(err) {
				console.error("上传错误:", err);
				this.$message.error("上传失败，请重试！");
			},
			handleSuccess(response, file, fileList) {
				if (response.flag === true) {
					this.fileList = fileList;
					// 改为将所有文件名保存到数组中
					this.form.contentCover = fileList.map(file => file.response.data).join(",");
					if (fileList.length > 3) {
						this.uploadDisabled = true;
					}
					this.$message.success(response.message);
				} else {
					this.$message.error(response.message);
				}
			},
			handleChange(file, fileList) {
				this.noneBtnImg = fileList.length >= this.limitCountImg;
			},
			handleRemove(file, fileList) {
				this.noneBtnImg = fileList.length >= this.limitCountImg;
				this.fileList.pop();
				this.uploadDisabled = false;
			},
			handlePreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			submitUpload() {
				this.$refs.upload.submit();
			},
			submitForm(form) {
				this.$refs[form].validate((valid) => {
					if (valid) {
						this.addPost();
						this.$refs.form.resetFields();
						this.editor.destroy();
						this.editor = null;
						console.log(this.form.sortId)
					} else {
						return false;
					}
				});
			},
		},
		created() {}

	};
</script>

<style lang="scss" scoped>

</style>